#ifndef __TIMER_MAP__
#define __TIMER_MAP__

#include<unordered_map>
#include<mutex>

//处理多线程下的哈希表线程安全
template<typename K,typename V>
class HashMap
{
public:
    void insert(const K& key,const V& val)
    {
        std::unique_lock<std::mutex> mtx(_hash_map_mutex);
        _hash_map[key]=val;
    }

    void insert(const K& key,const V&& val)
    {
        std::unique_lock<std::mutex> mtx(_hash_map_mutex);
        _hash_map[key]=std::move(val);
    }

    void erase(const K& key)
    {
        std::unique_lock<std::mutex> mtx(_hash_map_mutex);
        if(_hash_map.find(key)!=_hash_map.end())_hash_map.erase(key);
    }

    bool getval(const K& key,V val)
    {
        std::unique_lock<std::mutex> mtx(_hash_map_mutex);
        if(_hash_map.find(key)!=_hash_map.end())
        {
            val=_hash_map[key];
            return true;
        }
        return false;
    }

    bool count(const K& key)
    {
        std::unique_lock<std::mutex> mtx(_hash_map_mutex);
        return _hash_map.count(key);
    }

    size_t size()
    {
        std::unique_lock<std::mutex> mtx(_hash_map_mutex);
        return _hash_map.size();
    }

private:
    std::unordered_map<K,V> _hash_map;
    std::mutex _hash_map_mutex;
};

#endif